package com.benio.ycfdisplayer.fresco;

import android.graphics.ColorFilter;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;

import com.benio.ycfdisplayer.displayer.Options;
import com.facebook.drawee.drawable.ScalingUtils.ScaleType;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.imagepipeline.common.ImageDecodeOptions;
import com.facebook.imagepipeline.common.Priority;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.Postprocessor;

/**
 * {@link com.facebook.drawee.backends.pipeline.Fresco}加载配置
 * Created by zhangzhibin on 2016/9/2.
 */
public class FrescoDisplayOptions extends Options {
    public final ScaleType placeholderImageScaleType;
    public final ScaleType failureImageScaleType;
    public final Drawable retryImage;
    public final ScaleType retryImageScaleType;
    public final Drawable progressBarImage;
    public final ScaleType progressBarImageScaleType;
    public final Drawable backgroundImage;
    public final PointF actualImageFocusPoint;
    public final ColorFilter actualImageColorFilter;
    public final int fadeDuration;
    public final ScaleType actualImageScaleType;
    public final RoundingParams roundingParams;

    public final boolean tapToRetryEnabled;
    public final boolean autoPlayAnimations;
    public final boolean retainImageOnFailure;

    public final boolean autoRotateEnabled;
    public final boolean diskCacheEnabled;
    public final boolean progressiveRenderingEnabled;
    public final boolean localThumbnailPreviewsEnabled;
    public final ImageDecodeOptions imageDecodeOptions;
    public final ImageRequest.RequestLevel lowestPermittedRequestLevel;
    public final Priority requestPriority;
    public final Postprocessor postprocessor;
    public final ImageRequest.CacheChoice cacheChoice;
    public final ResizeOptions resizeOptions;

    public FrescoDisplayOptions(Builder builder) {
        super(builder);
        // GenericDraweeHierarchy settings
        placeholderImageScaleType = builder.placeholderImageScaleType;
        failureImageScaleType = builder.failureImageScaleType;
        retryImage = builder.retryImage;
        retryImageScaleType = builder.retryImageScaleType;
        progressBarImage = builder.progressBarImage;
        progressBarImageScaleType = builder.progressBarImageScaleType;
        backgroundImage = builder.backgroundImage;
        actualImageFocusPoint = builder.actualImageFocusPoint;
        actualImageColorFilter = builder.actualImageColorFilter;
        fadeDuration = builder.fadeDuration;
        actualImageScaleType = builder.actualImageScaleType;
        roundingParams = builder.roundingParams;

        // DraweeController settings
        tapToRetryEnabled = builder.tapToRetryEnabled;
        autoPlayAnimations = builder.autoPlayAnimations;
        retainImageOnFailure = builder.retainImageOnFailure;

        // ImageRequest settings
        autoRotateEnabled = builder.autoRotateEnabled;
        diskCacheEnabled = builder.diskCacheEnabled;
        progressiveRenderingEnabled = builder.progressiveRenderingEnabled;
        localThumbnailPreviewsEnabled = builder.localThumbnailPreviewsEnabled;
        imageDecodeOptions = builder.imageDecodeOptions;
        lowestPermittedRequestLevel = builder.lowestPermittedRequestLevel;
        requestPriority = builder.requestPriority;
        postprocessor = builder.postprocessor;
        cacheChoice = builder.cacheChoice;
        resizeOptions = builder.resizeOptions;
    }

    public static class Builder extends Options.Builder {
        private ScaleType placeholderImageScaleType;
        private ScaleType failureImageScaleType;
        private Drawable retryImage;
        private ScaleType retryImageScaleType;
        private Drawable progressBarImage;
        private ScaleType progressBarImageScaleType;
        private Drawable backgroundImage;
        private PointF actualImageFocusPoint;
        private ColorFilter actualImageColorFilter;
        private int fadeDuration;
        private ScaleType actualImageScaleType;
        private RoundingParams roundingParams;
        // 暂时不支持动态改变overlay
        //private List<Drawable> overlay;
        //private Drawable pressedStateOverlay;

        // DraweeController setttings
        private boolean tapToRetryEnabled;
        private boolean autoPlayAnimations;
        private boolean retainImageOnFailure;

        // ImageRequest settings
        private boolean diskCacheEnabled = true;
        private boolean autoRotateEnabled;
        private boolean progressiveRenderingEnabled;
        private boolean localThumbnailPreviewsEnabled;
        private ImageDecodeOptions imageDecodeOptions;
        private ImageRequest.RequestLevel lowestPermittedRequestLevel;
        private Priority requestPriority;
        private Postprocessor postprocessor;
        private ImageRequest.CacheChoice cacheChoice;
        private ResizeOptions resizeOptions;

        public Builder() {
            fadeDuration = GenericDraweeHierarchyBuilder.DEFAULT_FADE_DURATION;
            actualImageScaleType = GenericDraweeHierarchyBuilder.DEFAULT_ACTUAL_IMAGE_SCALE_TYPE;
            placeholderImageScaleType = GenericDraweeHierarchyBuilder.DEFAULT_SCALE_TYPE;
            failureImageScaleType = GenericDraweeHierarchyBuilder.DEFAULT_SCALE_TYPE;
            retryImageScaleType = GenericDraweeHierarchyBuilder.DEFAULT_SCALE_TYPE;
            progressBarImageScaleType = GenericDraweeHierarchyBuilder.DEFAULT_SCALE_TYPE;
//
//            imageDecodeOptions = ImageDecodeOptions.defaults();
//            lowestPermittedRequestLevel = ImageRequest.RequestLevel.FULL_FETCH;
//            requestPriority = Priority.HIGH;
//            cacheChoice = ImageRequest.CacheChoice.DEFAULT;
        }

        public Builder setPlaceholderImageScaleType(ScaleType placeholderImageScaleType) {
            this.placeholderImageScaleType = placeholderImageScaleType;
            return this;
        }

        public Builder setFailureImageScaleType(ScaleType failureImageScaleType) {
            this.failureImageScaleType = failureImageScaleType;
            return this;
        }

        /**
         * 使用时需将tapToRetryEnabled设置为true
         */
        public Builder setRetryImage(Drawable retryImage) {
            this.retryImage = retryImage;
            return this;
        }

        public Builder setRetryImageScaleType(ScaleType retryImageScaleType) {
            this.retryImageScaleType = retryImageScaleType;
            return this;
        }

        public Builder setProgressBarImage(Drawable progressBarImage) {
            this.progressBarImage = progressBarImage;
            return this;
        }

        public Builder setProgressBarImageScaleType(ScaleType progressBarImageScaleType) {
            this.progressBarImageScaleType = progressBarImageScaleType;
            return this;
        }

        public Builder setBackgroundImage(Drawable background) {
            this.backgroundImage = background;
            return this;
        }

        public Builder setFadeDuration(int fadeDuration) {
            this.fadeDuration = fadeDuration;
            return this;
        }

        public Builder setActualImageScaleType(ScaleType actualImageScaleType) {
            this.actualImageScaleType = actualImageScaleType;
            return this;
        }

        public Builder setActualImageFocusPoint(PointF actualImageFocusPoint) {
            this.actualImageFocusPoint = actualImageFocusPoint;
            return this;
        }

        public Builder setActualImageColorFilter(ColorFilter actualImageColorFilter) {
            this.actualImageColorFilter = actualImageColorFilter;
            return this;
        }

        public Builder setRoundingParams(RoundingParams roundingParams) {
            this.roundingParams = roundingParams;
            return this;
        }

        @Override
        public Builder setFailureImage(Drawable failureImage) {
            return (Builder) super.setFailureImage(failureImage);
        }

        @Override
        public Builder setPlaceHolderImage(Drawable placeHolderImage) {
            return (Builder) super.setPlaceHolderImage(placeHolderImage);
        }

        public Builder setTapToRetryEnabled(boolean tapToRetryEnabled) {
            this.tapToRetryEnabled = tapToRetryEnabled;
            return this;
        }

        public Builder setAutoPlayAnimations(boolean autoPlayAnimations) {
            this.autoPlayAnimations = autoPlayAnimations;
            return this;
        }

        public Builder setRetainImageOnFailure(boolean retainImageOnFailure) {
            this.retainImageOnFailure = retainImageOnFailure;
            return this;
        }

        public Builder setAutoRotateEnabled(boolean autoRotateEnabled) {
            this.autoRotateEnabled = autoRotateEnabled;
            return this;
        }

        public Builder setDiskCacheEnabled(boolean diskCacheEnabled) {
            this.diskCacheEnabled = diskCacheEnabled;
            return this;
        }

        public Builder setProgressiveRenderingEnabled(boolean progressiveRenderingEnabled) {
            this.progressiveRenderingEnabled = progressiveRenderingEnabled;
            return this;
        }

        public Builder setLocalThumbnailPreviewsEnabled(boolean localThumbnailPreviewsEnabled) {
            this.localThumbnailPreviewsEnabled = localThumbnailPreviewsEnabled;
            return this;
        }

        public Builder setImageDecodeOptions(ImageDecodeOptions imageDecodeOptions) {
            this.imageDecodeOptions = imageDecodeOptions;
            return this;
        }

        public Builder setLowestPermittedRequestLevel(ImageRequest.RequestLevel lowestPermittedRequestLevel) {
            this.lowestPermittedRequestLevel = lowestPermittedRequestLevel;
            return this;
        }

        public Builder setRequestPriority(Priority requestPriority) {
            this.requestPriority = requestPriority;
            return this;
        }

        public Builder setPostprocessor(Postprocessor postprocessor) {
            this.postprocessor = postprocessor;
            return this;
        }

        public Builder setCacheChoice(ImageRequest.CacheChoice cacheChoice) {
            this.cacheChoice = cacheChoice;
            return this;
        }

        public Builder setResizeOptions(ResizeOptions resizeOptions) {
            this.resizeOptions = resizeOptions;
            return this;
        }

        @Override
        public FrescoDisplayOptions build() {
            return new FrescoDisplayOptions(this);
        }
    }
}
